#include "widget_color.h"

#include <QMouseEvent>
#include <QtGui/QColorDialog>
#include <QtGui/QPainter>

WidgetColor::WidgetColor( QWidget* a_Parent /*= 0*/, Qt::WFlags a_Flags /*= 0*/ )
{
	
}

WidgetColor::~WidgetColor()
{

}

void WidgetColor::Pick(bool a_Side)
{
	QColor result;
	if (a_Side)
	{
		result = QColorDialog::getColor(m_Max, this, "Select a color");
		if (result.isValid()) { m_Max = result; }
	}
	else
	{
		result = QColorDialog::getColor(m_Min, this, "Select a color");
		if (result.isValid()) { m_Min = result; }
	}

	update();
}

void WidgetColor::mousePressEvent(QMouseEvent* a_Event)
{
	QPoint mouse_pos = a_Event->pos();
	float dist = mouse_pos.x();
	float halfwidth = (float)(width() / 2);
	bool result = (dist > halfwidth);
	Pick(result);
}

void WidgetColor::paintEvent( QPaintEvent* a_Event )
{
	QPainter painter(this);

	QLinearGradient grad(0, 0, width(), 0);
	grad.setColorAt(0.f, m_Min);
	grad.setColorAt(1.f, m_Max);

	painter.setBrush(QBrush(grad));

	painter.drawRect(
		QRect(0, 0, size().width(), size().height())
	);

	QPoint start(0, 0);
	QPoint end(10, 10);

	/*painter.fillRect(0,  0,  10, 10,  Qt::lightGray);
	painter.fillRect(10, 0,  10, 10,  Qt::lightGray);
	painter.fillRect(0,  10, 10, 10,  Qt::darkGray);
	painter.fillRect(10, 0,  10, 10,  Qt::darkGray);*/
}

QColor WidgetColor::GetColorMin()
{
	return m_Min;
}

QColor WidgetColor::GetColorMax()
{
	return m_Max;
}

void WidgetColor::SetColorMin( QColor a_Color )
{
	m_Min = a_Color;

	update();
}

void WidgetColor::SetColorMax( QColor a_Color )
{
	m_Max = a_Color;

	update();
}
